// pages/sendResume/sendResume.js
const db = wx.cloud.database();
import extension from '../../utils/getExtensionName/getExtensionName.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileInfo: {},
    detail: {},
    type: ''
  },
  //选择简历文件
  selectResume() {
    let that = this;
    wx.chooseMessageFile({
      count: 1,
      type: 'file',
      success(res) {
        // console.log(res);
        that.setData({
          fileInfo: res.tempFiles[0],
        });
        var tmp = (res.tempFiles[0].size / 1000000).toFixed(2);
        that.setData({
          ["fileInfo.size"]: tmp
        })
        console.log(that.data.fileInfo);

        let extensionName = extension.getExtensionName(that.data.fileInfo.name);
        console.log("文件扩展名：" + extensionName);
        if (extensionName == "pdf" || extensionName == 'doc' || extensionName == 'docx') {
          that.setData({
            type: extensionName
          });
        } else {
          wx.showModal({
            content: '当前文件不合法，只支持投递pdf、doc、docx类型的简历文件',
            showCancel: false
          });
          that.setData({
            fileInfo: {},
            type: ''
          });
          console.log(that.data.fileInfo);
        }

      },
      fail(res) {
        console.log(res);
      }
    });
  },

  //预览简历文件
  previewResume() {
    if (this.data.fileInfo.path == '' || this.data.fileInfo.path == undefined) {
      wx.showToast({
        title: '请选择文件',
        icon: 'error'
      })
    } else {
      let that = this;
      wx.openDocument({
        filePath: that.data.fileInfo.path,
        success: res => {
          console.log('预览成功');
        },
        fail: res => {
          console.log('预览失败');
        }
      })
    }

  },

  //上传简历文件
  uploadResume() {
    var that = this;
    if (getApp().personInfo.name == "") {
      wx.showModal({
        content: "请先在个人资料界面填写真实姓名",
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定')
            wx.navigateTo({
              url: '/pages/personInfo/personInfo',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return;
    }


    if (this.data.fileInfo.path == undefined) {
      wx.showToast({
        title: '请选择文件',
        icon: "error"
      })
      console.log("没有选择文件")
      return;
    }
    wx.requestSubscribeMessage({
      tmplIds: ['ZsVNF1tKYrk_isoYXtWlj2UCpAqAdlHYfDMLOTihRhY'],
      success: res => {
        console.log(res);
        if (res.ZsVNF1tKYrk_isoYXtWlj2UCpAqAdlHYfDMLOTihRhY == 'reject') {
          console.log('用户取消订阅');
          return;
        } else {
          console.log('用户确认订阅')
          db.collection('subscribeRecord').add({
            data: {
              subMsg: {
                thing1: {
                  value: ""
                },
                thing2: {
                  value: ""
                },
                time4: {
                  value: ''
                },
                thing5: {
                  value: ''
                }
              },
              status: 0, //发送状态 1表示已发送，0表示未发送
              createTime: (new Date()).toLocaleString(),
              templateId: 'ZsVNF1tKYrk_isoYXtWlj2UCpAqAdlHYfDMLOTihRhY', // 模板ID
              job_id: that.data.detail._id,
            },
            success: res => {
              console.log(res);
              wx.showLoading({
                title: '上传中',
              })

              console.log("fileInfo.time:" + that.data.fileInfo.time);
              console.log("fileInfo.path:" + that.data.fileInfo.path);
              wx.cloud.uploadFile({
                config: {
                  env: getApp().envId
                },
                filePath: that.data.fileInfo.path,
                cloudPath: 'resume/' + Date.now() + '_' + that.data.fileInfo.name, // 上传至云端的路径
                success: res1 => {
                  console.log(res1);
                  db.collection('resume').add({
                    data: {
                      fileID: res1.fileID,
                      receiver_ID: that.data.detail.HR_id,
                      sender_ID: getApp().personInfo._openid,
                      salary: that.data.detail.salary,
                      timestamp: (new Date()).toLocaleString(),
                      status: '待查看',
                      unit: that.data.detail.unit,
                      job_id: that.data.detail._id,
                      job_name: that.data.detail.jobName,
                      city: that.data.detail.city,
                      jobPicUrl: "https://636c-cloud1-1ga8zsp16c57584a-1308614351.tcb.qcloud.la/image/%E6%9D%91%20(1).svg?sign=6de9937fa2787bd0b6579fac9cf506b1&t=1652883768"
                    },
                    success: res2 => {
                      console.log(res2);
                      wx.hideLoading();
                      wx.showToast({
                        title: '上传成功',
                      });
                      console.log('简历文件[' + that.data.fileInfo.name + ']上传成功');
                      wx.redirectTo({
                        url: '/pages/sendSuccess/sendSuccess',
                      })
                    },
                    fail: res2 => {
                      wx.hideLoading();
                      console.log(res2);
                    }
                  });
                },
                fail: res => {
                  wx.hideLoading();
                  console.log(res);
                }
              })
            }
          })
        }

      }
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      detail: JSON.parse(options.detail)
    })
    console.log(this.data.detail)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})